import { resetRouter, routes } from "@/router";
import type { Profile } from "@/types/user";
import { getLocalToken, setLocalToken } from "@/utils/auth";
import { defineStore } from "pinia";
import { ref } from "vue";
import type { RouteRecordRaw } from "vue-router";

export const useUserStore = defineStore("hm-user", () => {
  const token = ref(getLocalToken() || "");
  // 个人信息
  const profile = ref<Profile>({} as Profile);
  const setProfile = (p: Profile) => {
    profile.value = p;
  };
  // 设置token
  const setToken = (t: string) => {
    token.value = t;
    // 本地存一份
    setLocalToken(t);
  };

  const clearUserInfo = () => {
    token.value = "";
    setLocalToken("");
  };

  const menuList = ref<RouteRecordRaw[]>([...routes]);
  const setMenuList = (filterRoutes: RouteRecordRaw[]) => {
    menuList.value = [...menuList.value, ...filterRoutes];
  };
  const resetMenu = () => {
    // 重置左侧菜单
    menuList.value = [...routes];
    // 重置路由系统
    resetRouter();
  };
  return {
    token,
    setToken,
    clearUserInfo,
    profile,
    setProfile,
    setMenuList,
    resetMenu,
  };
});
